package com.one.common.location_service;

import android.app.PendingIntent;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.net.Uri;
import android.os.IBinder;
import android.os.RemoteException;

import com.one.common.ILocationHelperServiceAIDL;
import com.one.common.ILocationServiceAIDL;
import com.one.common.config.CMemoryData;
import com.one.common.config.CPersisData;
import com.one.common.receiver.NotificationHandle;

import androidx.annotation.Nullable;

/**
 * Author: zhulunjun
 * Email: zhulunjun@qq.com
 * CreateDate: 2019/5/24 16:34
 * Description：
 **/
public class LocationHelperService extends Service {
    private ServiceUtils.CloseServiceReceiver mCloseReceiver;
    @Override
    public void onCreate() {
        super.onCreate();
        startBind();
        mCloseReceiver = new ServiceUtils.CloseServiceReceiver(this);
        registerReceiver(mCloseReceiver, ServiceUtils.getCloseServiceFilter());
    }

    @Override
    public void onDestroy() {
        if (mInnerConnection != null) {
            unbindService(mInnerConnection);
            mInnerConnection = null;
        }

        if (mCloseReceiver != null) {
            unregisterReceiver(mCloseReceiver);
            mCloseReceiver = null;
        }

        super.onDestroy();
    }

    private ServiceConnection mInnerConnection;
    private void startBind() {
        final String locationServiceName = "com.one.common.location_service.LocationService";
        mInnerConnection = new ServiceConnection() {

            @Override
            public void onServiceDisconnected(ComponentName name) {
                if(CPersisData.getIsLogin() && CMemoryData.isCar()) {
                    Intent intent = new Intent();
                    intent.setAction(locationServiceName);
                    startService(ServiceUtils.getExplicitIntent(getApplicationContext(), intent));
                }
            }

            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                ILocationServiceAIDL l = ILocationServiceAIDL.Stub.asInterface(service);
                try {
                    l.onFinishBind();
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
        };

        Intent intent = new Intent();
        intent.setAction(locationServiceName);
        intent.setComponent(new ComponentName(getPackageName(),locationServiceName));
        bindService(ServiceUtils.getExplicitIntent(getApplicationContext(), intent), mInnerConnection, Service.BIND_AUTO_CREATE);
    }

    private HelperBinder mBinder;

    private class HelperBinder extends ILocationHelperServiceAIDL.Stub {

        @Override
        public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {

        }

        @Override
        public void onFinishBind(int notiId) {
            Intent intentNotification = new Intent(Intent.ACTION_VIEW,
                    Uri.parse("ycpcarapp://openpage?actionAndroid=com.ycp.car.main.ui.activity.MainActivity&param1=1"));
            intentNotification.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
            PendingIntent intentPend = PendingIntent.getActivity(LocationHelperService.this, 0,
                    intentNotification, PendingIntent.FLAG_UPDATE_CURRENT);
            startForeground(notiId, new NotificationHandle(LocationHelperService.this).crate("货有友持续为您服务",
                    "点击返回应用", intentPend));
//            stopForeground(true);
        }
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        if (mBinder == null) {
            mBinder = new HelperBinder();
        }

        return mBinder;
    }
}
